package infiniteindicator.indicator;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import infiniteindicator.slideview.BaseSliderView;

public class RecycleAdapter extends PagerAdapter implements BaseSliderView.BitmapLoadCallBack {

  private List<BaseSliderView> mSliderViews;

  public RecycleAdapter(List<BaseSliderView> sliderViews) {
    mSliderViews = new ArrayList<>();
    mSliderViews.addAll(sliderViews);
  }

  public RecycleAdapter() {
    mSliderViews = new ArrayList<>();
  }

  @Override
  public int getCount() {
    return mSliderViews.size();
  }

  @Override
  public boolean isViewFromObject(View view, Object o) {
    return o == view;
  }

  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    View v = mSliderViews.get(position).getView();
    container.addView(v);
    return v;
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View)object);
  }

  public <T extends BaseSliderView> void addSlider(T slider) {
    slider.setOnImageLoadListener(this);
    mSliderViews.add(slider);
    notifyDataSetChanged();
  }

  public <T extends BaseSliderView> void removeSlider(T slider) {
    if (mSliderViews.contains(slider)) {
      mSliderViews.remove(slider);
      notifyDataSetChanged();
    }
  }

  public void removeSliderAt(int position) {
    if (mSliderViews.size() < position) {
      mSliderViews.remove(position);
      notifyDataSetChanged();
    }
  }

  public void removeAllSliders() {
    mSliderViews.clear();
    notifyDataSetChanged();
  }

  @Override
  public void onLoadStart(BaseSliderView target) {
  }

  @Override
  public void onLoadComplete(BaseSliderView target) {
  }

  @Override
  public void onLoadFail(BaseSliderView target) {
  }
}
